package org.easy.core.util;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;


public class RecursiveMap {
    public static void main(String[] args) {
        LinkedList<String> keys = getStrings("1.2.3.4");
        LinkedList<String> values = getStrings("a.b.c.d");

        Map<String, Object> map = new HashMap<>();
        recursivePut(map, keys, values, "对象");


        keys = getStrings("1.2.33.4");
        values = getStrings("a.b.c.d");
        recursivePut(map, keys, values, "对象");
        System.out.println(map);
    }

    private static LinkedList<String> getStrings(String s2) {
        LinkedList<String> keys = new LinkedList<>();
        for (String s : s2.split(KEY_LINK_CHAR)) {
            keys.add(s);
        }
        return keys;
    }

    private static final String KEY_LINK_CHAR = "\\.";

    public static <T> void recursivePut(Map<String, Object> map, LinkedList<String> keys, LinkedList<String> values, T t) {
        if (keys.size() != values.size()) {
            throw new RuntimeException("keys ,values 长度必须相等");
        }

        if (keys.size() == 0) {
            return;
        }

        if (keys.size() > 1) {
            map.putIfAbsent("key", keys.getFirst());
            map.putIfAbsent("name", values.getFirst());

            HashMap<String, Object> itemMap = new HashMap<>();
            map.put("chidren", itemMap);

            keys.removeFirst();
            values.removeFirst();
            recursivePut(itemMap, keys, values, t);
        } else {
            map.putIfAbsent("key", keys);
            map.putIfAbsent("name", values);
            map.put("target", t);
        }
    }
}
